package com.code.view.handlers;

import java.util.Hashtable;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

import com.code.plugin.LabelProperties;
import com.code.view.ViewConstant;
import com.code.view.ViewUtils;
import com.code.view.entity.HandlerVO;
import com.code.view.entity.PreferenceVO;
import com.code.view.entity.TableItemControl;
import com.code.view.listeners.AddMouseListener;
import com.code.view.listeners.CloseMouseListener;
import com.code.view.listeners.DeleteMouseListener;
import com.code.view.listeners.ExcuseMouseListener;
import com.code.view.listeners.FolderMouseListener;
import com.code.view.listeners.RadioGroupSelectListener;
import com.code.view.listeners.ViewLogMouseListener;


/**
 * Our sample handler extends AbstractHandler, an IHandler base class.
 *
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
public class CodeGeneratorHandler extends AbstractHandler {
    // 基本属性
    private Text dbUserName;
    private Text dbPassword;
    private Text dbUrl;
    private Combo dbDriver;
    private Text author;
    private Text entitySuffix;
    private Text commonPackage;
    private Text outPutPath;

    private Button commonCheckButton;
    private Button entityCheckButton;
    private Button serviceCheckButton;
    private Button pageCheckButton;
    private Button controllerCheckButton;
    
    private Button myBatisButton;
    private Button iBatisButton;
    private Button implButton;

    private Table table;

    private Shell shell;

    private String[][] contents = { { "0", "", "", "" } }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$

    private Hashtable<TableItem, TableItemControl> tableControls = new Hashtable<TableItem, TableItemControl>();


    /**
     * The constructor.
     */
    public CodeGeneratorHandler() {
    }


    /**
     * the command has been executed, so extract extract the needed information
     * from the application context.
     */
    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
        Display display = window.getWorkbench().getDisplay();
        shell = new Shell(display);
        shell.setText(LabelProperties.CodeGeneratorHandler_handlerTitle);
        shell.setImage(ViewUtils.getImage("codegenerator.bmp")); //$NON-NLS-1$
        shell.setBounds(100, 100, 900, 600);
        FormLayout layout = new FormLayout();
        layout.marginWidth = 0;
        layout.marginHeight = 0;
        shell.setLayout(layout);

        Composite topComposite = new Composite(shell, SWT.ALL);
        FormData topData = new FormData();
        topData.left = new FormAttachment(0, 0);
        topData.top = new FormAttachment(0, 0);
        topData.right = new FormAttachment(100, 0);
        topComposite.setLayoutData(topData);
        topComposite.setLayout(new GridLayout(8, true));

        GridData text7Style = new GridData(GridData.FILL, GridData.CENTER, true, false, 7, 1);
        GridData text3Style = new GridData(GridData.FILL, GridData.CENTER, true, false, 3, 1);
        GridData labelStyle = new GridData(GridData.FILL, GridData.CENTER, true, false, 1, 1);

        Label userNameLabel = new Label(topComposite, SWT.RIGHT);
        userNameLabel.setText(LabelProperties.CodeGenerator_dbUserName);
        userNameLabel.setLayoutData(labelStyle);
        dbUserName = new Text(topComposite, SWT.BORDER);
        dbUserName.setLayoutData(text3Style);

        Label passwordLabel = new Label(topComposite, SWT.RIGHT);
        passwordLabel.setText(LabelProperties.CodeGenerator_dbPassword);
        passwordLabel.setLayoutData(labelStyle);
        dbPassword = new Text(topComposite, SWT.BORDER);
        dbPassword.setLayoutData(text3Style);

        Label dbUrlLabel = new Label(topComposite, SWT.RIGHT);
        dbUrlLabel.setText(LabelProperties.CodeGenerator_dbUrl);
        dbUrlLabel.setLayoutData(labelStyle);
        dbUrl = new Text(topComposite, SWT.BORDER);
        dbUrl.setLayoutData(text7Style);

        Label dbDriverLabel = new Label(topComposite, SWT.RIGHT);
        dbDriverLabel.setText(LabelProperties.CodeGenerator_dbDriver);
        dbDriverLabel.setLayoutData(labelStyle);
        dbDriver = new Combo(topComposite, SWT.READ_ONLY);
        dbDriver.setItems(ViewConstant.DB_DRIVERS);
        dbDriver.setLayoutData(text3Style);

        Label authorLabel = new Label(topComposite, SWT.RIGHT);
        authorLabel.setText(LabelProperties.CodeGenerator_author);
        authorLabel.setLayoutData(labelStyle);
        author = new Text(topComposite, SWT.BORDER);
        author.setLayoutData(text3Style);

        Label entitySuffixLabel = new Label(topComposite, SWT.RIGHT);
        entitySuffixLabel.setText(LabelProperties.CodeGenerator_entitySuffix);
        entitySuffixLabel.setLayoutData(labelStyle);
        entitySuffix = new Text(topComposite, SWT.BORDER);
        entitySuffix.setLayoutData(text3Style);

        Label commonPackageLabel = new Label(topComposite, SWT.RIGHT);
        commonPackageLabel.setText(LabelProperties.CodeGenerator_commonPackage);
        commonPackageLabel.setLayoutData(labelStyle);
        commonPackage = new Text(topComposite, SWT.BORDER);
        commonPackage.setLayoutData(text3Style);

        Label outPutPathLabel = new Label(topComposite, SWT.RIGHT);
        outPutPathLabel.setText(LabelProperties.CodeGenerator_outPutPath);
        outPutPathLabel.setLayoutData(labelStyle);
        outPutPath = new Text(topComposite, SWT.BORDER);
        outPutPath.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 6, 1));

        Button folderButton = new Button(topComposite, SWT.PUSH);
        folderButton.setText(LabelProperties.CodeGeneratorHandler_buttion_select);
        folderButton.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false, 1, 1));

        Label checkLabel = new Label(topComposite, SWT.RIGHT);
        checkLabel.setText(LabelProperties.CodeGeneratorHandler_checkLabel);
        checkLabel.setLayoutData(labelStyle);

        Button allCheckButton = new Button(topComposite, SWT.CHECK);
        allCheckButton.setText(LabelProperties.CodeGeneratorHandler_checkButtion_all);
        allCheckButton.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false, 1, 1));

        commonCheckButton = new Button(topComposite, SWT.CHECK);
        commonCheckButton.setText(LabelProperties.CodeGeneratorHandler_checkButtion_common);
        commonCheckButton.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false, 2, 1));

        entityCheckButton = new Button(topComposite, SWT.CHECK);
        entityCheckButton.setText(LabelProperties.CodeGeneratorHandler_checkButtion_entity);
        entityCheckButton.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false, 2, 1));
        entityCheckButton.setSelection(true);

        serviceCheckButton = new Button(topComposite, SWT.CHECK);
        serviceCheckButton.setText(LabelProperties.CodeGeneratorHandler_checkButtion_service);
        serviceCheckButton.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false, 2, 1));
        serviceCheckButton.setSelection(true);

        Label spaceLabel = new Label(topComposite, SWT.RIGHT);
        spaceLabel.setText("");
        spaceLabel.setLayoutData(labelStyle);

        controllerCheckButton = new Button(topComposite, SWT.CHECK);
        controllerCheckButton.setText(LabelProperties.CodeGeneratorHandler_checkButtion_controller);
        controllerCheckButton.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false, 1, 1));
        controllerCheckButton.setSelection(true);

        pageCheckButton = new Button(topComposite, SWT.CHECK);
        pageCheckButton.setText(LabelProperties.CodeGeneratorHandler_checkButtion_page);
        pageCheckButton.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false, 6, 1));
        pageCheckButton.setSelection(true);
        
        Label ormLabel = new Label(topComposite, SWT.RIGHT);
        ormLabel.setText(LabelProperties.CodeGeneratorHandler_ormLabel);
        ormLabel.setLayoutData(labelStyle);
        
        myBatisButton=new Button(topComposite, SWT.RADIO);
        myBatisButton.setText(LabelProperties.CodeGeneratorHandler_ormButtion_mybatis);
        myBatisButton.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false, 1, 1));
        myBatisButton.setSelection(false);
        
        
        iBatisButton=new Button(topComposite, SWT.RADIO);
        iBatisButton.setText(LabelProperties.CodeGeneratorHandler_ormButtion_ibatis);
        iBatisButton.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false, 6, 1));
        iBatisButton.setSelection(true);
        
        Label implLabel = new Label(topComposite, SWT.RIGHT);
        implLabel.setText(LabelProperties.CodeGeneratorHandler_implLabel);
        implLabel.setLayoutData(labelStyle);
        
        implButton = new Button(topComposite, SWT.CHECK);
        implButton.setText(LabelProperties.CodeGeneratorHandler_implButtion);
        implButton.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false, 4, 1));
        implButton.setSelection(false);
        
        

        Label tableLabel = new Label(shell, SWT.LEFT);
        FormData tableLabelData = new FormData();
        tableLabelData.left = new FormAttachment(0, 10);
        tableLabelData.top = new FormAttachment(topComposite, 10);
        tableLabel.setText(LabelProperties.CodeGeneratorHandler_tableName);
        tableLabel.setFont(new Font(null, "Arial", 10, SWT.BOLD)); //$NON-NLS-1$
        tableLabel.setLayoutData(tableLabelData);

        Button addButton = new Button(shell, SWT.PUSH);
        FormData addData = new FormData();
        addData.right = new FormAttachment(100, -80);
        addData.top = new FormAttachment(topComposite);
        addData.width = 60;
        addButton.setLayoutData(addData);
        addButton.setText(LabelProperties.CodeGeneratorHandler_buttion_add);

        Button deleteButton = new Button(shell, SWT.PUSH);
        FormData deleteData = new FormData();
        deleteData.right = new FormAttachment(100, -10);
        deleteData.top = new FormAttachment(topComposite);
        deleteData.width = 60;
        deleteButton.setLayoutData(deleteData);
        deleteButton.setText(LabelProperties.CodeGeneratorHandler_buttion_delete);

        table = new Table(shell, SWT.BORDER);
        FormData tableData = new FormData();
        tableData.left = new FormAttachment(0, 10);
        tableData.top = new FormAttachment(deleteButton, 6);
        tableData.right = new FormAttachment(100, -10);
        table.setLayoutData(tableData);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        TableColumn selectColumn = new TableColumn(table, SWT.NONE);
        selectColumn.setWidth(50);
        selectColumn.setText(LabelProperties.CodeGeneratorHandler_table_title_select);

        TableColumn tableNameColumn = new TableColumn(table, SWT.NONE);
        tableNameColumn.setWidth(230);
        tableNameColumn.setText(LabelProperties.CodeGeneratorHandler_table_title_tableName);

        TableColumn classPathColumn = new TableColumn(table, SWT.NONE);
        classPathColumn.setWidth(230);
        classPathColumn.setText(LabelProperties.CodeGeneratorHandler_table_title_packagePath);

        TableColumn resultColumn = new TableColumn(table, SWT.NONE);
        resultColumn.setWidth(330);
        resultColumn.setText(LabelProperties.CodeGeneratorHandler_table_title_result);

        Button excuseButton = new Button(shell, SWT.PUSH);
        FormData excuseData = new FormData();
        excuseData.right = new FormAttachment(100, -150);
        excuseData.bottom = new FormAttachment(100, -10);
        excuseData.width = 60;
        excuseButton.setLayoutData(excuseData);
        excuseButton.setText(LabelProperties.CodeGeneratorHandler_buttion_excuse);

        Button viewLogButton = new Button(shell, SWT.PUSH);
        FormData viewLogData = new FormData();
        viewLogData.right = new FormAttachment(100, -80);
        viewLogData.bottom = new FormAttachment(100, -10);
        viewLogData.width = 60;
        viewLogButton.setLayoutData(viewLogData);
        viewLogButton.setText(LabelProperties.CodeGeneratorHandler_buttion_viewLog);

        Button closeButton = new Button(shell, SWT.PUSH);
        FormData closeData = new FormData();
        closeData.right = new FormAttachment(100, -10);
        closeData.bottom = new FormAttachment(100, -10);
        closeData.width = 60;
        closeButton.setLayoutData(closeData);
        closeButton.setText(LabelProperties.CodeGeneratorHandler_buttion_close);

        addButton.addMouseListener(new AddMouseListener(table, shell, tableControls));
        deleteButton.addMouseListener(new DeleteMouseListener(table, shell, tableControls));
        folderButton.addMouseListener(new FolderMouseListener(outPutPath, shell));
        excuseButton.addMouseListener(new ExcuseMouseListener(getData(), table, shell, tableControls));
        viewLogButton.addMouseListener(new ViewLogMouseListener(window));
        closeButton.addMouseListener(new CloseMouseListener(shell));

        allCheckButton.addSelectionListener(new RadioGroupSelectListener(allCheckButton, commonCheckButton, entityCheckButton,
            serviceCheckButton, pageCheckButton, controllerCheckButton));
        commonCheckButton.addSelectionListener(new RadioGroupSelectListener(allCheckButton, commonCheckButton, entityCheckButton,
            serviceCheckButton, pageCheckButton, controllerCheckButton));
        entityCheckButton.addSelectionListener(new RadioGroupSelectListener(allCheckButton, commonCheckButton, entityCheckButton,
            serviceCheckButton, pageCheckButton, controllerCheckButton));
        serviceCheckButton.addSelectionListener(new RadioGroupSelectListener(allCheckButton, commonCheckButton, entityCheckButton,
            serviceCheckButton, pageCheckButton, controllerCheckButton));
        pageCheckButton.addSelectionListener(new RadioGroupSelectListener(allCheckButton, commonCheckButton, entityCheckButton,
            serviceCheckButton, pageCheckButton, controllerCheckButton));
        controllerCheckButton.addSelectionListener(new RadioGroupSelectListener(allCheckButton, commonCheckButton, entityCheckButton,
            serviceCheckButton, pageCheckButton, controllerCheckButton));
        initData();

        shell.open();
        shell.layout();

        return shell;
    }


    private void initData() {
        PreferenceVO preferenceVO = ViewUtils.readPreferencePropertys();
        dbUserName.setText(preferenceVO.getDbUserName());
        dbPassword.setText(preferenceVO.getDbPassword());
        dbUrl.setText(preferenceVO.getDbUrl());
        dbDriver.setText(preferenceVO.getDbDriver());
        author.setText(preferenceVO.getAuthor());
        entitySuffix.setText(preferenceVO.getEntitySuffix());
        commonPackage.setText(preferenceVO.getCommonPackage());
        outPutPath.setText(preferenceVO.getOutPutPath());
        if(StringUtils.equals(ViewConstant.ORM_TYPE_MYBATIS, preferenceVO.getOrmType())){
        	myBatisButton.setSelection(true);
        }else{
        	iBatisButton.setSelection(true);
        }
        
        implButton.setSelection(false);
        
        
        contents[0][2] = preferenceVO.getPackagePath();

        for (String[] content : contents) {
            TableItem item = new TableItem(table, SWT.NONE);
            item.setText(content);
        }
        createCells(table);
    }


    private HandlerVO getData() {
        HandlerVO handlerVO = new HandlerVO();
        handlerVO.setAuthor(author);
        handlerVO.setCommonPackage(commonPackage);
        handlerVO.setDbDriver(dbDriver);
        handlerVO.setDbPassword(dbPassword);
        handlerVO.setDbUrl(dbUrl);
        handlerVO.setDbUserName(dbUserName);
        handlerVO.setEntitySuffix(entitySuffix);
        handlerVO.setOutPutPath(outPutPath);

        handlerVO.setIsCreateCommon(commonCheckButton);
        handlerVO.setIsCreateEntity(entityCheckButton);
        handlerVO.setIsCreateService(serviceCheckButton);
        handlerVO.setIsCreatePage(pageCheckButton);
        handlerVO.setIsCreateController(controllerCheckButton);
       handlerVO.setMyBatis(myBatisButton);
       handlerVO.setiBatis(iBatisButton);
       handlerVO.setImplButton(implButton);
        return handlerVO;
    }


    private void createCells(Table table) {
        for (int i = 0; i < table.getItemCount(); i++) {
            ViewUtils.createOneItemCells(table.getItem(i), table, tableControls);
        }
    }
}
